/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * QuanLyTau.java
 *
 * Created on Oct 25, 2012, 8:20:49 PM
 */
package Panel;

import DAO.TauDAO;
import DAO.TauDAO1;
import OBJECT.Tau;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.text.html.HTMLDocument.Iterator;

/**
 *
 * @author ngayngo9x
 */
public class QuanLyTau extends javax.swing.JPanel {

    /** Creates new form QuanLyTau */
    private DefaultTableModel tableModel;
    private static String themThanhCong = "Thêm tàu thành công";
    private static String suaThanhCong = "Sửa tàu thành công";
    private static String khongThanhCong = "Không thành công rồi";
    private static String xoaThanhCong = "Xóa tàu thành công";
    private ThongTinTau temp;
    private int m_MaTau;
    private int flagCapNhat;
    private String m_User;
    private String m_Pass;
    private String m_Port;
    private String m_Data;
    private String m_Host;
    
    public QuanLyTau(String User ,String Pass,String port,String DataBase,String Host) {
        KhoiTao(User, Pass, port, DataBase, Host);
        this.AnThongTinTau();
        this.m_User = User;
        this.m_Pass = Pass;
        this.m_Port = port;
        this.m_Data = DataBase;
        this.m_Host = Host;
    }
   
    private void LayDanhSach(String User ,String Pass,String port,String DataBase,String Host) {
        TauDAO tauDAO = new TauDAO(User, Pass, port, DataBase, Host);
        List<Tau> l = tauDAO.LayDanhSachTau();
        //System.out.print(l.size());
        tableModel = new DefaultTableModel();
        tableModel.addColumn("Mã Tàu");
        tableModel.addColumn("Tên Tàu");
        tableModel.addColumn("Loại Tàu");
        tableModel.addColumn("Tình Trạng");
        java.util.Iterator<Tau> i = l.iterator();
        while(i.hasNext()) {
            Object [] data = new Object[4];
            Tau t = i.next();
            data[0] = t.getMaTau();
            data[1] = t.getTenTau();
            data[2] = t.getLoaiTau();
            //System.out.print("hello");
            data[3] = t.getTinhTrang();
            tableModel.addRow(data);
        }
        jTable1.setModel(tableModel);
        tauDAO.CloseConnect();
    }
    
    private void KhoiTao(String User ,String Pass,String port,String DataBase,String Host) {
        initComponents();
        thongBaoLbl.setVisible(false);
        LayDanhSach(User, Pass, port, DataBase, Host);
        cboLoaiTau.removeAllItems();
        cboTinhTrang.removeAllItems();
        cboLoaiTau.addItem("Lớn");
        cboLoaiTau.addItem("Vừa");
        cboLoaiTau.addItem("Nhỏ");
        cboTinhTrang.addItem("Còn sử dụng");
        cboTinhTrang.addItem("Hết sử dụng");
    }
    
    private void AnThongTinTau() {
        jPanel1.setVisible(false);
    }
    
    private void HienThongTinTau() {
        jPanel1.setVisible(true);
    }
   /* 
    public void themDataToTable() {
        DefaultTableModel t = (DefaultTableModel)jTable1.getModel();
        t.fireTableDataChanged();
        temp.the
    }
     * 
     */

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        thongBaoLbl = new javax.swing.JLabel();
        themBtn = new javax.swing.JButton();
        xoaBtn = new javax.swing.JButton();
        suaBtn = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTenTau = new javax.swing.JTextField();
        cboTinhTrang = new javax.swing.JComboBox();
        cboLoaiTau = new javax.swing.JComboBox();
        btnNhapLại = new javax.swing.JButton();
        btnDongY = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("DANH SÁCH TÀU");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));
        add(jSeparator1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 300, 608, 10));

        thongBaoLbl.setText("Thông Tin Tàu");
        add(thongBaoLbl, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 310, -1, 20));
        thongBaoLbl.getAccessibleContext().setAccessibleName("ThongBaoLbl");

        themBtn.setText("Thêm");
        themBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                themBtnActionPerformed(evt);
            }
        });
        add(themBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 270, 100, -1));

        xoaBtn.setText("Xóa");
        xoaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoaBtnActionPerformed(evt);
            }
        });
        add(xoaBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, 270, 101, -1));

        suaBtn.setText("Sửa");
        suaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                suaBtnActionPerformed(evt);
            }
        });
        add(suaBtn, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 270, 100, -1));

        jPanel1.setLayout(null);

        jLabel2.setText("Tình Trạng");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(13, 80, 70, 14);

        jLabel3.setText("Tên Tàu");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(14, 20, 50, 14);

        jLabel4.setText("Loại Tàu");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(14, 50, 50, 14);
        jPanel1.add(txtTenTau);
        txtTenTau.setBounds(90, 20, 190, 20);

        cboTinhTrang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cboTinhTrang);
        cboTinhTrang.setBounds(90, 80, 190, 20);

        cboLoaiTau.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPanel1.add(cboLoaiTau);
        cboLoaiTau.setBounds(90, 50, 190, 20);

        btnNhapLại.setText("Nhập lại");
        jPanel1.add(btnNhapLại);
        btnNhapLại.setBounds(171, 110, 90, 23);

        btnDongY.setText("Đồng ý");
        btnDongY.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDongYActionPerformed(evt);
            }
        });
        jPanel1.add(btnDongY);
        btnDongY.setBounds(70, 110, 90, 23);

        jLabel5.setForeground(new java.awt.Color(255, 0, 51));
        jPanel1.add(jLabel5);
        jLabel5.setBounds(24, 144, 350, 20);

        add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 340, 390, 170));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 610, 220));
    }// </editor-fold>//GEN-END:initComponents

    private void suaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suaBtnActionPerformed
        thongBaoLbl.setText("Thêm Tàu Mới");
        thongBaoLbl.setVisible(true);
        int row = jTable1.getSelectedRow();
        int maTau = Integer.parseInt(jTable1.getValueAt(row, 0).toString());
        String tenTau = jTable1.getValueAt(row, 1).toString();
        //System.out.print(tenTau);
        int loaiTau = Integer.parseInt(jTable1.getValueAt(row, 2).toString());
        //System.out.print(loaiTau);
        String tinhTrang = jTable1.getValueAt(row, 3).toString();
        thongBaoLbl.setText("Sửa Tàu");
        this.LayDuLieuVuaChon();
        this.HienThongTinTau();
        flagCapNhat = 2;
        /*this.jPanel1.removeAll();
        temp = new ThongTinTau(maTau, tenTau, loaiTau, tinhTrang);
        temp.setVisible(true);
        this.jPanel1.add(temp);
        this.validate();
        this.repaint();*/
    }//GEN-LAST:event_suaBtnActionPerformed

    private void themBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_themBtnActionPerformed
        thongBaoLbl.setText("Thêm Tàu Mới");
        thongBaoLbl.setVisible(true);
        txtTenTau.setText("");
        cboLoaiTau.setSelectedIndex(0);
        cboTinhTrang.setSelectedIndex(0);
        this.HienThongTinTau();
        flagCapNhat = 1;
        /*temp = new ThongTinTau();
        temp.setVisible(true);
        this.jPanel1.add(temp);
        this.validate();
        this.repaint();*/
    }//GEN-LAST:event_themBtnActionPerformed

    private void LayDuLieuVuaChon() {
        int row = jTable1.getSelectedRow();
        //System.out.print(row);
        if(jTable1.getSelectedRowCount() >= 0) {
            int ma = Integer.parseInt(jTable1.getModel().getValueAt(row, 0).toString());
            this.m_MaTau = ma;
            txtTenTau.setText(jTable1.getModel().getValueAt(row, 1).toString());
            cboLoaiTau.setSelectedIndex((Integer.parseInt(jTable1.getModel().getValueAt(row, 2).toString())));
            cboTinhTrang.setSelectedIndex(Integer.parseInt(jTable1.getModel().getValueAt(row, 3).toString()));
        } else {
            thongBaoLbl.setText("Chọn tàu trong danh sách trước");
        }
    }
    
    private void xoaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xoaBtnActionPerformed
        thongBaoLbl.setText("Xóa Tuyến Đường");
        this.HienThongTinTau();
        this.LayDuLieuVuaChon();
        TauDAO td = new TauDAO(this.m_User, this.m_Pass, this.m_Port, this.m_Data, this.m_Host);
        int flagLoi = 0; 
        flagLoi = td.XoaTau(this.m_MaTau);
        if(flagLoi == 1) {
            jLabel5.setText(xoaThanhCong);
            this.LayDanhSach(m_User, m_Pass, m_Port, m_Data, m_Host);
        } else {
            jLabel5.setText(khongThanhCong);
        }
        td.CloseConnect();
    }//GEN-LAST:event_xoaBtnActionPerformed

    private void btnDongYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDongYActionPerformed
        String tenTau = txtTenTau.getText();
        int loaiTau = cboLoaiTau.getSelectedIndex();
        int trangThai = cboTinhTrang.getSelectedIndex();
        //trangThai++;
        TauDAO td = new TauDAO(this.m_User, this.m_Pass, this.m_Port, this.m_Data, this.m_Host);
        int flagLoi = 0;
        if(flagCapNhat == 1) {
            flagLoi = td.ThemTau(tenTau, loaiTau, trangThai + "");
            if(flagLoi == 1) {
                jLabel5.setText(themThanhCong);
                this.LayDanhSach(m_User, m_Pass, m_Port, m_Data, m_Host);
            } else {
                jLabel5.setText(khongThanhCong);
            }
        } else if(flagCapNhat == 2) {
            flagLoi = td.SuaTau(m_MaTau, tenTau, loaiTau, trangThai + "");
            if(flagLoi == 1) {
                jLabel5.setText(suaThanhCong);
                this.LayDanhSach(m_User, m_Pass, m_Port, m_Data, m_Host);
            } else {
                jLabel5.setText(khongThanhCong);
            }
        }
        td.CloseConnect();    
    }//GEN-LAST:event_btnDongYActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDongY;
    private javax.swing.JButton btnNhapLại;
    private javax.swing.JComboBox cboLoaiTau;
    private javax.swing.JComboBox cboTinhTrang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton suaBtn;
    private javax.swing.JButton themBtn;
    private javax.swing.JLabel thongBaoLbl;
    private javax.swing.JTextField txtTenTau;
    private javax.swing.JButton xoaBtn;
    // End of variables declaration//GEN-END:variables
}
